close all;
clear all;
clc;
%--------------------------------------------------------------------------

methods = ['FFT2'; 'SIZE'; 'DCT2'; 'HIST'; 'LIFE'];
in_base = 1:9;
variations = 10;
%--------------------------------------------------------------------------

methods = cellstr(methods);
test_vec = convert_to_index_in_base(generate_test_vector(in_base));
subp1 = 3; subp2 = 4;
%--------------------------------------------------------------------------

tic

for i = 1:size(methods)
    database{i} = generate_database(methods{i});
    toc
end

for m = 1:size(methods)
    
    curr = 1;
    corr1 = 0;  pc1 = []; good1 = []; bad1 = [];
    corr2 = 0;  pc2 = []; good2 = []; bad2 = [];
    corr3 = 0;  pc3 = []; good3 = []; bad3 = [];
    
    for n = 1:variations
        test_vec = convert_to_index_in_base(generate_test_vector(variations));
        new_database{m} = cut_test_images(database{m}, test_vec);
        
        for i = 1:length(test_vec)
            im_vec = database{m}(:, test_vec(i));
            [Y1 I1 Y2 I2 Y3 I3] = best_match(im_vec, new_database{m}, 10, 40);
            
            [dd ii] = get_dir_and_file(test_vec(i), 10);
            [d1 i1] = get_dir_and_file(I1(1), 10);
            [d2 i2] = get_dir_and_file(I2(1), 10);
            [d3 i3] = get_dir_and_file(I3(1), 10);
            
            [c1 corr1 good1 bad1 pc1 curr] = is_correct(corr1,good1,bad1,pc1,curr,dd,d1,Y1(1));
            [c2 corr2 good2 bad2 pc2 curr] = is_correct(corr2,good2,bad2,pc2,curr,dd,d2,Y2(1));
            [c3 corr3 good3 bad3 pc3 curr] = is_correct(corr3,good3,bad3,pc3,curr,dd,d3,Y3(1));
            
            best1 = best_five(c1, I1);
            best2 = best_five(c2, I2);
            best3 = best_five(c3, I3);

            figure(m); clf;           
            show_image(image_read(dd, ii, 'pgm'), subp1, subp2, 1, ['obraz: ' num2str(curr)]);
            show_method(subp1, subp2, 2, image_read(dd, ii, 'pgm'), methods{m});
            show_cplot(subp1, subp2, 3:4, pc1, pc2, pc3, 'zielony: L1', 'niebieski: L2', (corr1/curr)*100,(corr2/curr)*100 , (corr3/curr)*100);
                        
            show_image(best1, subp1, subp2, 5:6, ['(L1) poprawnych: ' num2str(corr1)]);    
            show_hist(subp1, subp2, 7:8, good1, bad1); 

            show_image(best2, subp1, subp2, 9:10, ['(L2) poprawnych: ' num2str(corr2)]);       
            show_hist(subp1, subp2, 11:12, good2, bad2); 
            
            %show_image(best3, subp1, subp2, 13:14, ['poprawnych: ' num2str(corr3)]);       
            %show_hist(subp1, subp2, 15:16, good3, bad3);             
            
            curr=curr+1;
            
            
           
        end
    end
    toc;
end

toc;

% im = image_read(11,1,'pgm');
% [vec im2] = image2vector(im, 'LIFE');
% figure(2);
% clf;
% imshow(im2);
% pause;